import sympy as sp
from sympy.abc import t
import numpy as np

class Point():
  """
  运动学意义上的点
  Attrs:
    rx、ry、rvx、rvy、rax、ray，这些变量都是关于时间t的函数
    x、y、vx、vy、ax、ay，均为浮点型的变量，对应于某一特定时刻点的运动
  """
  def __init__(self,rx=None,ry=None):
    """
    r:位置方程
    """
    # assert rx and ry
    
    self.symbol_rx = rx
    self.symbol_ry = ry

    self.rx = sp.lambdify(t,rx, 'numpy')  # 转换为numpy函数
    if(isinstance(ry,int)):
      self.rvx = lambda x:0
      self.rax = lambda x:0
    else:
      self.rvx = sp.lambdify(t,rx.diff(t), 'numpy')
      self.rax = sp.lambdify(t,rx.diff(t,2), 'numpy')

    self.ry = sp.lambdify(t,ry, 'numpy') 
    if(isinstance(ry,int)):
      self.rvy = lambda x:0  
      self.ray = lambda x:0 
    else:
      self.rvy = sp.lambdify(t,ry.diff(t), 'numpy')
      self.ray = sp.lambdify(t,ry.diff(t,2), 'numpy')
    
    self.update(0)
  
  def update(self,time):
    """
    用于根据传入的时间设置第二类参数的值,单位s
    """
    # <lambdifygenerated-18>:2: RuntimeWarning: invalid value encountered in arcsin
    # 出现这个提示一般是因为出现了0/0导致的
    # TODO:解决除0错误
    self.x = self.rx(time)
    self.y = self.ry(time)
    self.vx = self.rvx(time)
    self.vy = self.rvy(time)
    self.ax = self.rax(time)
    self.ay = self.ray(time)
    
  def __str__(self):
    return "(%.2f,%.2f)" % (self.x,self.y)
    # 等价于 "({:.2f},{:%.2f})".format(self.x,self.y)